<?php
$langCount = count($this->languages);
?>
<header class="page-title-bar">
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item active">
          <a href="#!"><i class="breadcrumb-icon fa fa-angle-left mr-2"></i>Forms</a>
        </li>
      </ol>
    </nav>
    <h1 class="page-title"> Basic Elements </h1>
</header>
<div class="page-section">
    <div class="d-xl-none">
      <button class="btn btn-danger btn-floated" type="button" data-toggle="sidebar"><i class="fa fa-th-list"></i></button>
    </div>
    <section id="base-style" class="card">
      <div class="card-body">
      <form id="<?php echo $this->controller;
?>_form" class="defaultForm form-horizontal <?php echo $this->controller;
?>" action="/api/<?php echo $this->controller;?>/v1/<?php echo $this->id ? 'id/'.$this->id: '';
?>" method="<?php echo $this->id ? 'PUT' : 'POST';
?>" novalidate>
<fieldset>
<?php if (isset($this->fields['title'])) {?><legend><?php echo $this->fields['title'];?></legend><?php }?>
<input type="hidden" name="token" value="<?php echo $_SESSION[APP_NAME]['security']['token'];?>" />
<?php foreach($this->fields['data'] ?? [] as $index => $fieldSet) {?>
<div class="panel" id="fieldSet_<?php echo $index;?>_0_0">
<?php foreach($fieldSet['form'] as $key => $field) {

////////////////////////////////////////////////////// one
if ($key === 'legend') {?>
<div class="panel-heading"><?php
if (isset($field['image']) && isset($field['title'])) {
?><img src="<?php echo $field['image'];?>" alt="<?php echo $field['title'];?>" /><?php
}
if (isset($field['icon'])) {
?><i class="<?php echo $field['icon'];?>"></i><?php
}
echo $field['title'];?></div>
<?php

////////////////////////////////////////////////////// two
} elseif ($key === 'description'){?>
<div class="alert alert-info"><?php echo $field;?></div>
<?php

////////////////////////////////////////////////////// three
}elseif ($key === 'warning'){?>
<div class="alert alert-warning"><?php echo $field;?></div>
<?php

////////////////////////////////////////////////////// four
}elseif ($key === 'success'){?>
<div class="alert alert-success"><?php echo $field;?></div>
<?php

////////////////////////////////////////////////////// five
}elseif ($key === 'error'){?>
<div class="alert alert-danger"><?php echo $field;?></div>
<?php

////////////////////////////////////////////////////// six
} elseif ($key === 'desc') {?>
<div class="alert alert-info col-lg-offset-3">
<?php
    if (is_array($field)) {
        foreach($field as $key => $value) {
            if (is_array($value)) {?>
<span<?php
                if (isset($value['id'])) {?> id="<?php
                    echo $value['id'];?>"<?php
                }
?>><?php
                    echo $value['text'];?></span><br />
<?php       } else {
                    echo $value;
                    if (isset($field[$key+1])) {?><br /><?php }
            }
        }
    } else {
        echo $field;
    }
?>
</div>
<?php

////////////////////////////////////////////////////// seven
}elseif ($key === 'input'){?>
<div class="form-wrapper">
<?php foreach($field as $input) {?>
<div class="form-group <?php echo $input['class'] ?? '';
if ($input['type'] === 'hidden') {?> hide<?php }?>">
<?php
if ($input['type'] === 'hidden') {
    require __DIR__.'/form/hidden.phtml';
} else {
    if (isset($input['label'])) {?>
        <label class="control-label col-lg-3"><?php
            if (isset($input['hint'])) {
        ?><span class="label-tooltip" data-toggle="tooltip" data-html="true" title="<?php
                foreach((array) $input['hint'] as $value) {
                    echo $value;
                }?>"><?php
            }
        echo $input['label'];
        if (isset($input['hint'])) {?></span><?php }
        if (!empty($input['required']) && $input['type'] !== 'radio') {
        ?> <span class="badge badge-danger">Required</span><?php
        }?></label>
        <?php
    }
?>
<div class="col-lg-<?php echo $input['col'] ?? 9;if (!isset($input['label'])) {?> col-lg-offset-3<?php }?>">
<?php
require __DIR__.'/form/'.$input['type'].'.phtml';

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ALL INPUT COMMON
if (isset($input['desc'])) {?>
<p class="form-text text-muted">
<?php
    if (is_array($input['desc'])) {
        foreach($input['desc'] as $value) {
            if (is_array($value)) {?>
<span id="<?php echo $value['id'];?>"><?php
            echo $value['text'];?></span><br />
<?php
            } else {
                echo $value;?><br />
<?php
            }
        }
    } else {
        echo $input['desc'];
    }
?>
</p>
<?php
}
?>
</div>
<?php }?>
</div>
<?php }?>
</div>
<?php
}}

//#################################################### COMMON BUTTON
if (isset($fieldSet['form']['submit']) || isset($fieldSet['form']['buttons'])) {?>
<div class="panel-footer"><?php
    if (isset($fieldSet['form']['submit'])) {
?><button type="submit" id="<?php
        echo $this->controller.'_submit';
        ?>" name="<?php
        echo $fieldSet['form']['submit']['name'] ?? $this->controller.'_submit';
        ?>" class="<?php
        echo $fieldSet['form']['submit']['class'] ?? 'btn btn-secondary';?>"><?php
        if (isset($fieldSet['form']['submit']['icon'])) {
        ?><i class="<?php
            echo $fieldSet['form']['submit']['icon'];?>"></i><?php
        }
        echo $fieldSet['form']['submit']['title'];
?></button>
<?php
    }
    if (!empty($this->showCancel)) {
?><a href="javascript:window.history.back();" id="<?php
        echo $this->controller.'_cancel';
    ?>" class="btn btn-danger"><?php echo l('app.cancel');?></a>
<?php
    }
    if (isset($fieldSet['form']['reset'])) {
?><button type="reset" id="<?php
        echo $this->controller.'_reset';
        ?>" class="<?php
        echo $fieldSet['form']['reset']['class'] ?? 'btn btn-secondary';?>"><?php
        if (isset($fieldSet['form']['reset']['icon'])) {
        ?><i class="<?php
            echo $fieldSet['form']['reset']['icon'];?>"></i><?php
        }
        echo $fieldSet['form']['reset']['title'] ?? l('app.reset');
?></button>
<?php
    }
    foreach($fieldSet['form']['buttons'] ?? [] as $btn) {
        if (isset($btn['href'])) {?>
<a href="<?php echo $btn['href'];?>"<?php
            if (isset($btn['id'])) {?> id="<?php echo $btn['id'];?>"<?php }
?> class="<?php echo $btn['class'] ?? 'btn btn-secondary';?>"<?php
            if (isset($btn['js'])) {?> onclick="<?php echo $btn['js'];?>"<?php }
?>><?php
            if (isset($btn['icon'])) {?><i class="<?php echo $btn['icon'];?>" ></i><?php }
            echo $btn['title'];
?></a>
<?php
        } else {
?><button type="<?php
            echo $btn['type'] ?? 'button';?>"<?php
            if (isset($btn['id'])) {?> id="<?php echo $btn['id'];?>"<?php }
?> class="<?php echo $btn['class'] ?? 'btn btn-secondary';?>" name="<?php
            echo $btn['name'] ?? 'submit'.$this->controller;?>"<?php
            if (isset($btn['js'])) {?> onclick="<?php echo $btn['js'];?>"<?php }
?>><?php
            if (isset($btn['icon'])) {?><i class="<?php echo $btn['icon'];?>" ></i><?php }
            echo $btn['title'];
?></button>
<?php
        }
    }
?></div><?php
}
?></div><?php }?></fieldset></form>
      </div>
    </section>
</div>
<script type="text/javascript"></script>